import Vue from 'vue'
import Router from 'vue-router'
import ProductsManage from '@/components/ProductsManage'
import LoginStart from '@/components/LoginStart'
import Login from '@/components/Login'

Vue.use(Router)
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
 
const originalReplace = Router.prototype.replace
Router.prototype.replace = function replace (location) {
  return originalReplace.call(this, location).catch(err => err)
}
const router=new Router({
    routes: [
        {
            path: '/',
            redirect:'/login',
        },
        {path: '/login',
        name: 'Login',
        component: Login},
        {
            path: '/home',
            redirect:'/welcome',
            name: 'LoginStart',
            component: LoginStart,
            children:[
                {
                    path:'/welcome',
                    component:()=>import('@/components/Welcome')
                },
                {
                    path:'/lunbo',
                    name:'Lunbo',
                    component:()=>import('@/components/Lunbo')
                },
                {
                    path: '/productsManage',
                    name: 'ProductsManage',
                    component: ProductsManage
                },{
                    path: '/cart',
                    name: 'Cart',
                    component: ()=>import('@/components/Cart')
                },
                {
                    path: '/demo',
                    name: 'demo',
                    component: ()=>import('@/components/demo')
                },
                {
                    path: '/orders',
                    name: 'Order',
                    component: ()=>import('@/components/Order')
                }
            ]
        }
        
    ]
})

//需要拿到路由的实例对象后进行，挂载路由导航守卫
router.beforeEach((to,from,next)=>{
    if(to.path==='/login') return next();
    //获取token
    const getToken=window.sessionStorage.getItem('token');
    //强制跳转
    if(!getToken) return next('/login');
    next();
})
export default router;
